<!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: wuhao
 * @Date: 2021-08-19 14:14:10
 * @LastEditors: wuhao
 * @LastEditTime: 2021-08-25 16:15:31
-->
<template>
  <div>
    <el-col :span="24">
      <el-menu :default-active="defaultActive"
        class="el-menu-vertical-demo"
        @select="handleSelect"
        unique-opened
        background-color="#d3dce6"
        text-color="#409EFF"
        active-text-color="#F56C6C"
        router>
        <Menus :routerList="menuData"></Menus>
      </el-menu>
    </el-col>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import Menus from '@/components/sideMenu/Menus.vue';
export default {
  name: 'SideMenu',
  components: {
    Menus
  },
  computed: {
    ...mapState('sideMenu', ['menuData'])
  },
  data() {
    return {
      defaultActive: '/home'
    }
  },
  // 点击路由标签时，菜单选中项变更
  watch: {
    $route(to, from) {
      from
      this.defaultActive = to.path
    }
  },
  methods: {
    ...mapActions('tabsView', ['addView']),
    // 点击菜单项时，新增路由标签和路由标签缓存
    handleSelect(key, keyPath, route) {
      let nameArr = key.split('/')
      let name = nameArr[nameArr.length - 1]
      let tab = { path: key, name: name, title: route.$el.innerText }
      this.addView({ tab })
    }
  }
}
</script>

<style  lang="css" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>